﻿using System;
using System.Linq;
using System.Web.Security;
using EmitMapper;
using InnovaApp.Layers.CommonLayer;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Portals.MVC4Portal.Models.MetaModels.ContactManagement;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.MVCHelpers;
using System.Web.Mvc;

namespace InnovaApp.Portals.MVC4Portal.Controllers
{
    /// <summary>
    /// Classe de base dont doivent hériter tous les controleurs
    /// </summary>
    [HandleError, CacheManager(enCacheMode.NoCache)]
    public abstract class _Controllers_Base<T> : InnovaApp.Layers.CommonLayer.Controllers._Controllers_Base<T> where T : INNOVACALL.Framework.Common.Models._Models_Base
    {
        internal ContactViewer_MetaModel CurrentContactGet(DataRepositories _dataContext)
        {
            string eMethod = eMethodBase + "(_Controllers_Base)CurrentContactGet";
            object eParameters = new { @_dataContext = "[DataContext]" };

            ContactViewer_MetaModel toReturn = new ContactViewer_MetaModel();
            Contact dbContact = _dataContext.InnoAppUser_Rep.GetById((Guid)Membership.GetUser().ProviderUserKey).Contact.FirstOrDefault();
            if (dbContact == null)
            {
                throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, Resources.View_AdvancedSamples_OrderWithDetails_CustomerMustBeSetBefore);
            }
            toReturn = ObjectMapperManager.DefaultInstance.GetMapper<Contact, ContactViewer_MetaModel>().Map(dbContact);
            toReturn.Genre = StringEnum.GetStringValue(EnumTools.EnumTypeTo<enGenres>(dbContact.FK_Genre));
            return toReturn;
        }
    }
}